/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.command.listener.exe;

import cef.command.Command;
import cef.command.listener.exe.CommandExeEvent;
import cef.command.listener.exe.CommandExeListener;
import cef.command.listener.exe.FigureEditorExeComponent;
import coalescent.cef.command.RunRecursionCommand;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.xy.DefaultXYDataset;

import java.awt.*;
import java.util.Arrays;
import java.util.List;

/**
 * @author Susanta Tewari
 */
public class VisualizeRecursionCacheDistn extends FigureEditorExeComponent {

    private JFreeChart chart;
    private DefaultXYDataset data_set;
    private double[][] data;
    private static final Stroke stroke = new BasicStroke(6.0f);

    @Override
    public void initComponents(final Command cmd) {

        final RunRecursionCommand command = (RunRecursionCommand) cmd;
        final NumberAxis x_axis           = new NumberAxis("Cut-Set");
        final NumberAxis y_axis           = new NumberAxis("CacheSize");

        x_axis.setAutoRangeIncludesZero(true);
        y_axis.setAutoRangeIncludesZero(true);
        x_axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());    // to integer units
        y_axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        data_set = new DefaultXYDataset();
        data     = create_data(command.getCurrentRecursionCacheSize().size());

        data_set.addSeries(0, data);

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);
        final XYPlot xy_plot                  = new XYPlot(data_set, x_axis, y_axis, renderer);

        renderer.setSeriesStroke(0, stroke);
        renderer.setSeriesPaint(0, new Color(255, 182, 193));    // light pink

        title  = "Recursion Cache Distribution";
        chart  = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, xy_plot, false);
        figure = new ChartPanel(chart);
    }

    private double[][] create_data(int size) {

        final double[][] data = new double[2][size];

        Arrays.fill(data[0], Double.NaN);
        Arrays.fill(data[1], Double.NaN);

        return data;
    }

    @Override
    public void lastUpdate(CommandExeEvent event) {
        updateComponentData(event);
    }

    @Override
    public void updateComponentData(final CommandExeEvent event) {

        final RunRecursionCommand command = (RunRecursionCommand) event.getSource();
        final List<Integer> cacheSize     = command.getCurrentRecursionCacheSize();
        final String rowKey               = "CacheSize";

        for (int i = 0; i < cacheSize.size(); i++) {

            data[0][i] = i;
            data[1][i] = cacheSize.get(i);
        }

        data_set.addSeries(0, data);
        chart.getXYPlot().datasetChanged(new DatasetChangeEvent(this, data_set));
    }

    @Override
    public Class<? extends Command> getTargetCommandClass() {
        return RunRecursionCommand.class;
    }

    @Override
    public CommandExeListener newInstance() {
        return new VisualizeRecursionCacheDistn();
    }
}
